package com.example.lagouk8s.controller;

import com.example.lagouk8s.entity.User;
import com.example.lagouk8s.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2020-11-29 20:16:00
 */
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Autowired
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(Integer id) {
        return this.userService.queryById(id);
    }

    @PostMapping("save")
    public String saveUser(User user){
        if(user == null){
            return "fail";
        }
        userService.insert(user);
        return "success";
    }

    @PostMapping("update")
    public String updateUser(User user){
        if(user == null || StringUtils.isEmpty(user.getId())){
            return "fail";
        }
        userService.update(user);
        return "success";
    }

    @PostMapping("delete")
    public String deleteUser(String userId){
        if(StringUtils.isEmpty(userId)){
            return "fail";
        }
        userService.deleteById(Integer.valueOf(userId));
        return "success";
    }
}